package cn.tedu.csmall.commons.restful;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class JsonPage<T> implements Serializable {
    //根据实际需求，添加分页信息属性
    @ApiModelProperty(value = "总页数",name = "totalPages")
    private Integer totalPages;
    @ApiModelProperty(value = "总条数",name = "totalCount")
    private Long totalCount;
    @ApiModelProperty(value = "页码",name = "pageNum")
    private Integer pageNum;
    @ApiModelProperty(value = "每页条数",name = "pageSize")
    private Integer pageSize;
    //分页数据
    @ApiModelProperty(value = "分页数据",name = "list")
    private List<T> list;

    //将PageInfo转成JsonPage
    public static <T> JsonPage<T> restPage(PageInfo<T> pageInfo){
        JsonPage<T> jsonPage = new JsonPage<>();
        jsonPage.setTotalPages(pageInfo.getPages());
        jsonPage.setTotalCount(pageInfo.getTotal());
        jsonPage.setPageNum(pageInfo.getPageNum());
        jsonPage.setPageSize(pageInfo.getPageSize());
        jsonPage.setList(pageInfo.getList());
        return jsonPage;
    }
}
